package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.dao.PaymentDAO;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class PaymentController {

    @Autowired
    private PaymentDAO paymentDAO;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/get/{id}")
    public CommonResult get(@PathVariable int id){
        Payment payment=paymentDAO.selectByPrimaryKey(id);
        if (payment==null){
            return new CommonResult(444,"没有该id的相关数据,serverPort:"+serverPort);
        }else {
            return new CommonResult(200,"success,serverPort:"+serverPort,payment);
        }
    }

    @PostMapping("/payment/insert")
    public CommonResult get(@RequestBody Payment payment){
        int res=paymentDAO.insert(payment);
        if (res <= 0){
            return new CommonResult(444,"插入数据失败,serverPort:"+serverPort);
        }else {
            return new CommonResult(200,"success,serverPort:"+serverPort,payment);
        }
    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @GetMapping(value = "/payment/getTimeOut")
    public String getTimeOut() throws InterruptedException {
        TimeUnit.SECONDS.sleep(3);
        return serverPort;
    }

}
